package com.qiantu.phone.ui.shotview;

import android.content.Context;
import android.graphics.Color;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.qiantu.phone.R;
import com.qiantu.api.entity.ControlDataColorTemperatureBean;
import com.qiantu.phone.ui.adapter.ColorTemperatureAdapter;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.widget.ColorTemperatureSeekBar;
import com.qiantu.phone.widget.RoundSeekBar;

import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.KELVIN;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.LIGHT;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_OFF;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_ON;

/**
 * @Author luzx
 * @Date 2022/1/10
 */
public class ColorTemperatureLightShotView extends BaseDeviceActView {

    private RoundSeekBar roundSeekBar;
    private RecyclerView colorTemperatureRecyclerView;
    private ColorTemperatureSeekBar colorTemperatureSeekBar;
    private ColorTemperatureAdapter mColorTemperatureAdapter;
    private ControlDataColorTemperatureBean mColorTemperatureInfo;
    private ControlDataColorTemperatureBean.ControlData controlData;

    public ColorTemperatureLightShotView(Context context) {
        super(context);
    }

    public ColorTemperatureLightShotView(Context context, String deviceType) {
        super(context, deviceType);
    }

    @Override
    public int getLayoutId() {
        return R.layout.color_temperature_shot_view;
    }

    @Override
    public void initView() {
        roundSeekBar = findViewById(R.id.round_seek_bar);
        roundSeekBar.setOnChangeListener(new RoundSeekBar.OnChangeListener(){
            @Override
            public void onProgressChanged(int value) {

            }

            @Override
            public void onStopTrackingTouch(int value) {
                if(value == 0){
                    addAct(POWER, POWER_OFF);
                    colorTemperatureSeekBar.setOpenState(false);
                    if(mColorTemperatureAdapter != null){
                        mColorTemperatureAdapter.clearSelected();
                    }
                    //没有亮度时删除亮度动作指令
                    removeAct(LIGHT);
                    //当关闭时，删除色温动作指令
                    removeAct(KELVIN);
                }else{
                    addAct(POWER, POWER_ON);
                    addAct(LIGHT, value + "");
                    colorTemperatureSeekBar.setOpenState(true);
                    int colorTemp = colorTemperatureSeekBar.getColorTemp();
                    if(colorTemp > 0){
                        setKelvinItem(colorTemp);
//                        addAct(KELVIN, colorTemp + "");
                    }
                }
            }

            @Override
            public void onSwitch(boolean isOpen) {
                if(isOpen){
                    addAct(POWER, POWER_ON);
                    if(roundSeekBar.getProgress() != 0){
                        addAct(LIGHT, roundSeekBar.getProgress() + "");
                    }else{
                        //当开时，没有亮度时删除亮度动作指令
                        removeAct(LIGHT);
                    }
                    colorTemperatureSeekBar.setOpenState(true);
                    int colorTemp = colorTemperatureSeekBar.getColorTemp();
                    if(colorTemp > 0){
                        setKelvinItem(colorTemp);
                        addAct(KELVIN, colorTemp + "");
                    }
                }else{
                    addAct(POWER, POWER_OFF);
                    if(mColorTemperatureAdapter != null){
                        mColorTemperatureAdapter.clearSelected();
                    }
                    //当关闭时，删除亮度动作指令
                    removeAct(LIGHT);
                    //当关闭时，删除色温动作指令
                    removeAct(KELVIN);
                }
                colorTemperatureSeekBar.setOpenState(isOpen);
            }
        });
        colorTemperatureRecyclerView = findViewById(R.id.color_temperature_recycler_view);
        colorTemperatureSeekBar = findViewById(R.id.color_temperature_seek_bar);
        colorTemperatureSeekBar.setOnSeekBarChangeListener(new ColorTemperatureSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onValueChanged(int colorTemp) {

            }

            @Override
            public void onStartTrackingTouch() {

            }

            @Override
            public void onStopTrackingTouch(int colorTemp) {
                setKelvinItem(colorTemp);
                addAct(KELVIN, colorTemp + "");
            }
        });
        mColorTemperatureInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataColorTemperatureBean.class);
        controlData = mColorTemperatureInfo.getControlData();
        colorTemperatureSeekBar.setMinMaxValue(controlData.getColourtemperature_min(), controlData.getColourtemperature_max());
        if(controlData.getColorData() != null && controlData.getColorData().size() > 0){
            mColorTemperatureAdapter = new ColorTemperatureAdapter(getContext());
            mColorTemperatureAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    if(roundSeekBar.isOpen()){
                        mColorTemperatureAdapter.selectItem(position);
                        ControlDataColorTemperatureBean.ColorData colorData = mColorTemperatureAdapter.getData().get(position);
                        colorTemperatureSeekBar.setColorTemp(colorData.getValue());
                        addAct(KELVIN, colorData.getValue() + "");
                    }
                }
            });
            colorTemperatureRecyclerView.setAdapter(mColorTemperatureAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), controlData.getColorData().size());
            colorTemperatureRecyclerView.setLayoutManager(gridLayoutManager);
            mColorTemperatureAdapter.setData(controlData.getColorData());
            int[] gradientColors = new int[controlData.getColorData().size()];
            for(int i=0; i<controlData.getColorData().size(); i++){
                ControlDataColorTemperatureBean.ColorData colorData = controlData.getColorData().get(i);
                String color = colorData.getColor_16();
                gradientColors[i] = Color.parseColor(color);
            }
            colorTemperatureSeekBar.setGradientColors(gradientColors);
        }
    }

    private void setKelvinItem(int kelvin){
        if(controlData.getColorData() != null){
            boolean hasSame = false;
            for(int i=0; i<controlData.getColorData().size(); i++){
                ControlDataColorTemperatureBean.ColorData colorData = controlData.getColorData().get(i);
                if(colorData.getValue() == kelvin){
                    mColorTemperatureAdapter.selectItem(i);
                    hasSame = true;
                    break;
                }
            }
            if(!hasSame){
                mColorTemperatureAdapter.clearSelected();
            }
        }
    }

    @Override
    public void updateView() {
        //默认空的话，添加默认动作开
        if(mDeviceActMap.size() == 0){
            addAct(POWER, POWER_ON);
        }
        //根据已保存动作指令集合更新UI
        roundSeekBar.setOpenState(POWER_ON.equals(getState(POWER, "0")));
        roundSeekBar.setProgress(Integer.valueOf(getState(LIGHT, "0")));
        int kelvin = Integer.valueOf(getState(KELVIN, "0"));
        colorTemperatureSeekBar.setOpenState(roundSeekBar.isOpen());
        colorTemperatureSeekBar.setColorTemp(kelvin);
        setKelvinItem(kelvin);
    }
}
